using UnityEngine;
using DG.Tweening;
using TMPro;
/// <summary>
/// 伤害飘字
/// </summary>
public class UIShowText : MonoBehaviour
{
    private TMP_Text text;
    public float jumpPower = 2f;//跳跃力度
    public float duration = 1f;//持续时间
    Vector3 defaultLocalScale;

    private void Awake()
    {
        text = GetComponent<TMP_Text>();
        defaultLocalScale = transform.localScale;
    }

    public void SetText(string str, Color color, Transform tf)
    {
        text.text = str;
        text.color = color;
        //如果要做人物头上生成，可以适当加上人物的图片高度
        // transform.position = new Vector2(tf.position.x, tf.position.y + tf.GetComponent<SpriteRenderer>().sprite.bounds.size.y);
        transform.position = new Vector2(tf.position.x + Random.Range(-0.5f, 0.5f), tf.position.y);
        transform.localScale = defaultLocalScale;
        gameObject.SetActive(true);

        transform.DOScale(transform.localScale * 2, duration / 2).From(transform.localScale).SetLoops(2, LoopType.Yoyo);//先变大再变小
        text.DOFade(1f, duration / 2).From(0f).SetLoops(2, LoopType.Yoyo);//先渐显再渐隐
        //上跳效果
        transform.DOJump(transform.position, jumpPower, 1, duration).OnComplete(()=>{
            // Destroy(gameObject);
            ObjectPool.PushObject(gameObject);
        });
    }
}
